require 'time'

class Movie
  include Comparable
  attr_reader :name
  attr_reader :showtime
  def initialize(name,showtime)
    @name = name
    if showtime.is_a?(String)
    @showtime = Time.parse(showtime)
    else
    @showtime = showtime
    end
  end
  def name_and_showtime
    print self.name
    if self.showtime <= Time.now
       print ": already started at "
       if self.showtime.hour < 12
          print self.showtime.hour
          print ":"
          if self.showtime.min < 10
             print "0"
          end
          print self.showtime.min
          print " AM\n"
       else
          print self.showtime.hour - 12
          print ":"
          if self.showtime.min < 10
             print "0"
          end
          print self.showtime.min
          print " PM\n"
       end
    else
       print ": next show time at "
       if self.showtime.hour < 12
          print self.showtime.hour
          print ":"
          if self.showtime.min < 10
             print "0"
          end
          print self.showtime.min
          print " AM\n"
       else
          print self.showtime.hour - 12
          print ":"
          if self.showtime.min < 10
             print "0"
          end
          print self.showtime.min
          print " PM\n"
       end
    end
  end
  def <=>(mov)
    if self.showtime < mov.showtime
      return -1
    end
    if self.showtime == mov.showtime
      return 0
    end
    if self.showtime > mov.showtime
      return 1
    end
  end
end

movies = []
movies << Movie.new("Orphan", Time.parse("1:00 pm"))
movies << Movie.new("Orphan", Time.parse("4:05 pm"))
movies << Movie.new("Orphan", Time.parse("7:00 pm"))
movies << Movie.new("GI Joe", Time.parse("1:05 pm"))
movies << Movie.new("GI Joe", Time.parse("4:30 pm"))
movies << Movie.new("GI Joe", Time.parse("6:50 pm"))
movies << Movie.new("Rocky Horror", Time.parse("12:00 am"))
movies << Movie.new("District 9", Time.parse("11:00 am"))
movies << Movie.new("District 9", Time.parse("3:00 pm"))
movies << Movie.new("District 9", "10:22 pm")

movies[1].name_and_showtime
movies[9].name_and_showtime
print movies[9] <=> movies[2]
print "\n"
print movies[1].between?(movies[2],movies[3])
print "\n"
movies.sort!
for n in (0..9)
movies[n].name_and_showtime
end
print "It worked! Since I mixed the Comparable module into the Movie class, it automatically inherits all of the methods that are possible to use on a comparable object, so it took my rocket operator and used it to sort the movies in ascending showtime order. "